package com.orange.delivery.presentation.page;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.context.SecurityContextHolder;

import com.orange.delivery.presentation.common.HeaderFooterTemplatePage;

public class LogoutPage extends HeaderFooterTemplatePage {

	private static final long serialVersionUID = -9029748747538352173L;
	private static final Logger LOG = LoggerFactory.getLogger(LogoutPage.class);

	public LogoutPage(PageParameters parameters) {
		super();
		createLogoutPageTitle();
		createReconnectButton();
		invalidateSessionAndSpringContext();
	}

	private void createLogoutPageTitle() {
		Label logoutPageTitle = new Label("logoutPageTitle", getString("logout.page.title"));
		add(logoutPageTitle);
	}

	private void invalidateSessionAndSpringContext() {
		LOG.debug("deleting wicket session and spring security context");
		/*
		 * Be careful : the logout page must be stateless to avoid creation of a new session (this would generate an endless loop)
		 */
		getSession().invalidate();
		SecurityContextHolder.getContext().setAuthentication(null);
	}

	private void createReconnectButton() {
		BookmarkablePageLink<Void> reconnectButton = new BookmarkablePageLink<Void>("reconnectButton", HomePage.class);
		add(reconnectButton);
	}

}
